//package cn.fintecher.pangolin.service.repair.config;
//
//import cn.fintecher.pangolin.common.model.OperatorModel;
//import cn.fintecher.pangolin.common.model.response.SystemLogModel;
//import cn.fintecher.pangolin.common.utils.Snowflake;
//import cn.fintecher.pangolin.common.web.SystemLogConfig;
//import cn.fintecher.pangolin.entity.domain.SystemLog;
//import cn.fintecher.pangolin.service.repair.respository.SystemLogRepository;
//import cn.fintecher.pangolin.service.repair.service.DataRepairBaseService;
//import org.apache.commons.lang.StringUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Objects;
//
///**
// * @Author: huyanmin
// * @Description: 系统日志
// * @Date 2018/10/09
// */
//@Aspect
//@Order(1)
//@Component
//public class SystemLogAop {
//
//    private final static String AFTER = "after";
//    private final static String EXCEPTION = "exception";
//    private final Logger log = LoggerFactory.getLogger(SystemLogAop.class);
//    ThreadLocal<Long> startTime = new ThreadLocal<>();
//
//    @Autowired
//    private SystemLogConfig systemLogConfig;
//
//    @Autowired
//    private SystemLogRepository systemLogRepository;
//
//    @Autowired
//    private DataRepairBaseService dataRepairBaseService;
//
//    private Snowflake snowflake=new Snowflake((int) Thread.currentThread().getId()%1024);
//
//    // 切入点*Controller
//    @Pointcut("execution(public * cn.fintecher.pangolin.*.*.web.*Controller.*(..))")
//    public void systemLogAop() {
//    }
//
//    @Before("systemLogAop()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        startTime.set(System.currentTimeMillis());
//        log.info("进入的方法的时间戳" + System.currentTimeMillis());
//    }
//
//    @After("systemLogAop()")
//    public void doAfter(JoinPoint joinPoint) throws Throwable {
//        SystemLogModel systemLogModel = systemLogConfig.saveSystemLogsBlock(joinPoint, AFTER, null, startTime);
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String tokenStr = request.getHeader("X-UserToken");
//        if (StringUtils.isNotBlank(tokenStr)) {
//            OperatorModel operator = dataRepairBaseService.getUserByToken(tokenStr);
//            if (Objects.nonNull(systemLogModel)) {
//                SystemLog systemLog = new SystemLog();
//                BeanUtils.copyProperties(systemLogModel, systemLog);
//                systemLog.setOperator(operator.getUsername());
//                systemLog.setId(String.valueOf(snowflake.next()));
//                systemLogModel.setOperatorName(operator.getFullName());
//                systemLogRepository.save(systemLog);
//            }
//        }
//    }
//
//
//    /**
//     * 异常通知 用于拦截记录异常日志
//     *
//     * @param joinPoint
//     * @param e
//     */
//    @AfterThrowing(pointcut = "systemLogAop()", throwing = "e")
//    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
//        SystemLogModel systemLogModel = systemLogConfig.saveSystemLogsBlock(joinPoint, EXCEPTION, e, startTime);
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String tokenStr = request.getHeader("X-UserToken");
//        try {
//            if (StringUtils.isNotBlank(tokenStr)) {
//                OperatorModel operator = dataRepairBaseService.getUserByToken(tokenStr);
//                if (Objects.nonNull(systemLogModel)) {
//                    SystemLog systemLog = new SystemLog();
//                    BeanUtils.copyProperties(systemLogModel, systemLog);
//                    systemLog.setOperator(operator.getUsername());
//                    systemLog.setId(String.valueOf(snowflake.next()));
//                    systemLogModel.setOperatorName(operator.getFullName());
//                    systemLogRepository.save(systemLog);
//                }
//            }
//        }catch (exception exception){
//            exception.printStackTrace();
//        }
//
//    }
//}
